package mae.app;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DepressionGlassAdapter {
	public static final String KEY_ROWID = "_id";
	public static String KEY_PATTERN = "Pattern";
	public static final String KEY_COLOR = "Color";
	public static final String KEY_PIECE = "Piece";
	public static final String KEY_VALUE = "Value";
	public static final String KEY_QUANTITY = "Quantity";
	private static final String GLASS_TABLE = "DepressionGlass";
	private Context context;
	private SQLiteDatabase db;
	private CollectionsDatabaseHelper dbHelper;
	private String americanSweetheart = "American Sweetheart";
	private String holiday = "Holiday";
	private String madrid = "Madrid";
	private String vitrock = "Vitrock";
	private String monax = "Monax";
	private String pink = "Pink";
	private String miscolor = "Miscolor";

	public DepressionGlassAdapter(Context context) {
		this.context = context;
	}

	public DepressionGlassAdapter open() throws SQLException {
		dbHelper = new CollectionsDatabaseHelper(context);
		db = dbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		dbHelper.close();
	}

	/**
	 * Update the Glass item
	 */
	public boolean updateGlass(long rowId, String value) {
		ContentValues values = createContentValues(value);
		return db.update(GLASS_TABLE, values, KEY_ROWID + "=" + rowId, null) > 0;
	}

	/**
	 * Return a Cursor over the list of all monax AmericanSweetheart
	 * 
	 * @return Cursor over all monax american sweetheart
	 */
	public Cursor fetchAllMonaxAmericanSweetheart() {
		return db.query(false, GLASS_TABLE, new String[] { KEY_ROWID,
				KEY_PIECE, KEY_VALUE, KEY_QUANTITY }, KEY_PATTERN + "="
				+ americanSweetheart + "and" + KEY_COLOR + "=" + monax, null,
				null, null, null, null);
	}

	/**
	 * Return a Cursor over the list of all pink AmericanSweetheart
	 * 
	 * @return Cursor over all pink american sweetheart
	 */
	public Cursor fetchAllPinkAmericanSweetheart() {
		return db.query(GLASS_TABLE, new String[] { KEY_ROWID, KEY_PIECE,
				KEY_VALUE, KEY_QUANTITY }, KEY_PATTERN + "="
				+ americanSweetheart + "and" + KEY_COLOR + "=" + pink, null,
				null, null, null);
	}

	/**
	 * Return a Cursor over teh list of all pink Holiday
	 * 
	 * @return Cursor over all pink Holiday
	 */
	public Cursor fetchAllPinkHoliday() {
		return db.query(GLASS_TABLE, new String[] { KEY_ROWID, KEY_PIECE,
				KEY_VALUE, KEY_QUANTITY }, KEY_PATTERN + "=" + holiday + "and"
				+ KEY_COLOR + "=" + pink, null, null, null, null);
	}

	/**
	 * Return a Cursor over the list of all miscolor Holiday
	 * 
	 * @return Cursor over all miscolor Holiday
	 */
	public Cursor fetchAllMiscolorHoliday() {
		return db.query(GLASS_TABLE, new String[] { KEY_ROWID, KEY_PIECE,
				KEY_VALUE, KEY_QUANTITY }, KEY_PATTERN + "=" + holiday + "and"
				+ KEY_COLOR + "=" + miscolor, null, null, null, null);
	}

	/**
	 * Return a Cursor over the list of all Madrid
	 * 
	 * @return Cursor over all Madrid
	 */
	public Cursor fetchAllMadrid() {
		return db.query(GLASS_TABLE, new String[] { KEY_ROWID, KEY_PIECE,
				KEY_VALUE, KEY_QUANTITY }, KEY_PATTERN + "=" + madrid, null,
				null, null, null);
	}

	public Cursor fetchAllVitrock() {
		return db.query(GLASS_TABLE, new String[] { KEY_ROWID, KEY_PIECE,
				KEY_VALUE, KEY_QUANTITY }, KEY_PATTERN + "=" + vitrock, null,
				null, null, null);
	}

	/**
	 * Return a Cursor positioned at the defined Glass
	 */
	public Cursor fetchGlass(long rowId) throws SQLException {
		Cursor mCursor = db.query(GLASS_TABLE, new String[] { KEY_PIECE,
				KEY_VALUE, KEY_QUANTITY }, KEY_ROWID + "=" + rowId, null, null,
				null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	/**
	 * Puts the inputed value into the database
	 * 
	 * @param value
	 * @return
	 */
	private ContentValues createContentValues(String value) {
		ContentValues values = new ContentValues();
		values.put(KEY_VALUE, value);
		return values;
	}
}
